//
//  HomeVCViewController.m
//  K8彩票
//
//  Created by zhoubo on 2017/6/6.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "HomeVC.h"

#import "Lotterys.h"
#import "UIViewController+HUD.h"
#import "MBProgressHUD.h"
#import <objc/runtime.h>
#import "Config.h"
#import "CaipiaoHistoryModel.h"

#import "KuaiVC.h"
#import "PaiLieVC.h"
#import "PaiLie5VC.h"
#import "IssueModel.h"

#import "ShuangSeVC.h"
#import "DaLeTouVC.h"


#define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]

#define randomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))


@interface HomeVC ()

@property (nonatomic,strong) CaipiaoHistoryModel* model;

@property (nonatomic,weak) Lotterys* lotterys;


@property (nonatomic,assign) NSInteger issueNo;

@end

@implementation HomeVC


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setup];
     [self requestCaipiaoList:@{@"appkey":APP_KEY,@"caipiaoid":@"75"} caipiaoid:75];
    
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notice:) name:@"fast3" object:nil];
    

    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"alert"]) {
        [self alertController];

    }
    



    
}

-(void)notice:(NSNotification*)sender
{
    if ([sender.userInfo[@"vc"] containsString: @"fast3"]) {
        KuaiVC* vc = [KuaiVC new];
        [self presentViewController:vc animated:YES completion:nil];
        
        if ([sender.userInfo[@"vc"] containsString:@"jiangsu"]) {
            //75
            vc.kind = 75;
        }
        else if ([sender.userInfo[@"vc"] containsString:@"hubei"])
        {
            //80
            vc.kind = 80;
            
        }
        else if ([sender.userInfo[@"vc"] containsString:@"anhui"])
        {
            //76
            vc.kind = 76;
        }

    }
    else if ([sender.userInfo[@"vc"] isEqualToString: @"pailie3"])
    {
        [self.navigationController pushViewController:[PaiLieVC new] animated:YES];
    }
    else if([sender.userInfo[@"vc"] isEqualToString: @"pailie5"])
    {
        [self.navigationController pushViewController:[PaiLie5VC new] animated:YES];
    }
    else if ([sender.userInfo[@"vc"] isEqualToString: @"shuangse"])
    {
        [self.navigationController pushViewController:[ShuangSeVC new] animated:YES];
    }
    else if ([sender.userInfo[@"vc"] isEqualToString: @"daletou"])
    {
        [self.navigationController pushViewController:[DaLeTouVC new] animated:YES];
    }
 
}



-(void)setup
{
    Lotterys* lotterys = [[Lotterys alloc]initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height+20)];
    
    [self.view addSubview:lotterys];
    self.lotterys = lotterys;

    
    
    
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    ((ViewController *)[UIApplication sharedApplication].keyWindow.rootViewController).mainTabbar.frame = CGRectMake(0, ScreenHeight() - 49, ScreenWidth(), 49);
    self.navigationController.navigationBarHidden = YES ;
    
    [UIView animateWithDuration:0.7 animations:^{
        ViewController* con = [UIApplication sharedApplication].keyWindow.rootViewController;
        con.mainTabbar.alpha = 1;
    }];
}


/// 请求开奖信息列表
/// 双色球11 大乐透14 排列三16 排列五17 江苏快三75 湖北快三80 安徽快3 76

- (void)requestCaipiaoList:(NSDictionary *)parameters caipiaoid:(NSInteger )caipiaoid{
    //    [self showHudInView:self.view hint:@""];
    [[HttpManager getInstance]get:BASE_URL params:parameters successCallback:^(NSString *state, id responseObject) {
        
        NSDictionary *result = [responseObject objectForKey:@"result"];
        
        CaipiaoHistoryModel *model = [[CaipiaoHistoryModel alloc]initWithDictionary:result];
        
        self.model = model;
        
        
        //        [self hideHud];
        
    } failCallback:^(NSString *state, id responseObject) {
        //        [self hideHud];
    }];
}



-(void)setModel:(CaipiaoHistoryModel *)model
{
    _model = model;
    
    NSString* issuetext = [model.issueno substringFromIndex:9];
    
    self.issueNo = issuetext.integerValue;
    
    
    
    NSString* number1 = [model.number substringToIndex:1];
    NSString* number2 = [model.number substringWithRange:NSMakeRange(2, 1)];
    NSString* number3 = [model.number substringFromIndex:4];
    
    
    
    NSLog(@"%@---%@---%@",model.issueno,model.number,model.opendate);
    NSLog(@"%ld---%ld---%ld",model.issueno.length,model.number.length,model.opendate.length);
    
    // 通知betview显示图片
    
    IssueModel * issueModel = [[IssueModel alloc]init];
    issueModel.issue = model.issueno;
    issueModel.num1 = number1.integerValue;
    issueModel.num2 = number2.integerValue;
    issueModel.num3 = number3.integerValue;
    
    self.lotterys.model = issueModel;
    
//    [self.imgview1 stopAnimating];
//    [self.imgview2 stopAnimating];
//    [self.imgview3 stopAnimating];
//
//    self.imgview1.image = [UIImage imageNamed:number1];
//    self.imgview2.image = [UIImage imageNamed:number2];
//    self.imgview3.image = [UIImage imageNamed:number3];
    
    
    
//    
//    NSArray* arr = [model.opendate componentsSeparatedByString:@" "];
//    NSArray* ymd = [arr[0] componentsSeparatedByString:@"-"];
//    NSArray* hms = [arr[1] componentsSeparatedByString:@":"];
    
//    NSString* minute = hms.lastObject;
    
}

-(void)setIssueNo:(NSInteger)issueNo
{
    _issueNo = issueNo;
    
    // 通知betview显示期数
//    self.issueLabel.text = [NSString stringWithFormat:@"%ld期开奖",issueNo];
//    self.nextIssueLabel.text = [NSString stringWithFormat:@"距%ld期截止",issueNo + 1];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)alertController
{
    // 危险操作:弹框提醒
    // 1.UIAlertView
    // 2.UIActionSheet
    // iOS8开始:UIAlertController == UIAlertView + UIActionSheet
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"此app仅提供获取和展示各类彩票的开奖信息的功能,不引导并且不提供购买彩票的功能,如果想要购买彩票请到实体彩票代售点购买.所有数据信息仅供参考." preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加按钮
    __weak typeof(alert) weakAlert = alert;
    [alert addAction:[UIAlertAction actionWithTitle:@"了解" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
//        NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"取消", @"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
//        NSLog(@"点击了取消按钮");
    }]];
    
//    [alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        NSLog(@"点击了其它按钮");
//    }]];
    
//    // 添加文本框
//    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//        textField.textColor = [UIColor redColor];
//        textField.text = @"123";
//        [textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
//        //        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
//    }];
//    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//        textField.secureTextEntry = YES;
//        textField.text = @"123";
//    }];
 
    
    // 先不显示alert
    [self presentViewController:alert animated:YES completion:nil];
    [[NSUserDefaults standardUserDefaults] setObject:@1 forKey:@"alert"];
    
}

@end
